package com.cgnpc.scp.run.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.cuddemo.utils.IDHelper;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.exception.ScpException;
import com.cgnpc.scp.common.utils.StringUtil;
import com.cgnpc.scp.run.mybatis.entity.*;
import com.cgnpc.scp.run.mybatis.mapper.client.*;
import com.cgnpc.scp.run.service.ProjectInfoService;
import com.cgnpc.scp.run.service.ProjectUndertakeService;
import com.cgnpc.scp.run.utils.CommonRunUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**********************************
 * 用途说明: 项目承揽service
 * 作者姓名: zhangyibing
 * 创建时间: 2020年9月14日17:12:53
 ***********************************/
@Service("projectUndertakeService")
public class ProjectUndertakeServiceImpl implements ProjectUndertakeService {

    @Autowired
    private CurrentUser currentUser;

    @Autowired
    private ProjectInfoService projectInfoService;

    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    @Autowired
    private ProjectTeamMapper projectTeamMapper;

    @Autowired
    private ProjectReviewMapper projectReviewMapper;

    @Autowired
    private ProjectWbsMapper projectWbsMapper;

    @Autowired
    private ProjectDirectorMakingMapper projectDirectorMakingMapper;

    @Autowired
    private ProjectTenderOfferMapper projectTenderOfferMapper;

    @Autowired
    private ProjectMemberMapper projectMemberMapper;

    @Autowired
    private ProjectHtMapper projectHtMapper;

    @Override
    public void updateProjectStage(String projectInfoId){
        projectInfoMapper.updateProjectStage(projectInfoId);
    }

    @Override
    public void saveOrUpdateMembers(List<ProjectMember> members, String stageName, String projectId){
        projectMemberMapper.delete(new QueryWrapper<ProjectMember>()
                .eq("projectid", projectId)
                .eq("stage_table_name", stageName));
        for(ProjectMember member : members){
            member.setId(IDHelper.uuid());
            member.setProjectid(projectId);
            member.setStageTableName(stageName);
            projectMemberMapper.insert(member);
        }
    }

    /**
     *  组建承揽团队
     */
    @Override
    public AjaxResult saveOrUpdateTeam(ProjectTeam projectTeam) {
        String projectInfoId = projectTeam.getProjectid();
        if(StringUtil.isNullOrSpace(projectInfoId)){
            throw new ScpException("projectid can not be null");
        }
        String stageTableName = "tbl_run_project_team";
        String stageId = projectInfoService.getStageIdByProjectId(stageTableName, projectInfoId);
        if(StringUtil.isNullOrSpace(stageId)){
            projectTeamMapper.insert((ProjectTeam) CommonRunUtil.saveOrUpdateData(projectTeam, currentUser));
            updateProjectStage(projectInfoId);
        }else{
            projectTeam.setId(stageId);
            projectTeamMapper.updateById(
                    (ProjectTeam) CommonRunUtil.saveOrUpdateData(projectTeam, currentUser)
            );
        }
        List<ProjectMember> members = projectTeam.getMembers();
        if(members != null && members.size() != 0){
            saveOrUpdateMembers(members, stageTableName, projectInfoId);
        }

        return AjaxResult.success();
    }

    /**
     *  项目评审
     */
    @Override
    public AjaxResult saveOrUpdateReview(ProjectReview projectReview) {
        String projectInfoId = projectReview.getProjectid();
        if(StringUtil.isNullOrSpace(projectInfoId)){
            throw new ScpException("projectid can not be null");
        }
        String stageTableName = "tbl_run_project_review";
        String stageId = projectInfoService.getStageIdByProjectId(stageTableName, projectInfoId);
        if(StringUtil.isNullOrSpace(stageId)){
            projectReviewMapper.insert((ProjectReview) CommonRunUtil.saveOrUpdateData(projectReview, currentUser));
            updateProjectStage(projectInfoId);
        }else{
            projectReview.setId(stageId);
            projectReviewMapper.updateById(
                    (ProjectReview) CommonRunUtil.saveOrUpdateData(projectReview, currentUser)
            );
        }
        List<ProjectMember> members = projectReview.getMembers();
        if(members != null && members.size() != 0){
            saveOrUpdateMembers(members, stageTableName, projectInfoId);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult wbs(ProjectWbs projectWbs) {
        String projectInfoId = projectWbs.getProjectid();
        if(StringUtil.isNullOrSpace(projectInfoId)){
            throw new ScpException("projectid can not be null");
        }
        String stageTableName = "tbl_run_project_wbs";
        String stageId = projectInfoService.getStageIdByProjectId(stageTableName, projectInfoId);
        if(StringUtil.isNullOrSpace(stageId)){
            projectWbsMapper.insert((ProjectWbs) CommonRunUtil.saveOrUpdateData(projectWbs, currentUser));
            updateProjectStage(projectInfoId);
        }else{
            projectWbs.setId(stageId);
            projectWbsMapper.updateById(
                    (ProjectWbs) CommonRunUtil.saveOrUpdateData(projectWbs, currentUser)
            );
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult directorMaking(ProjectDirectorMaking projectDirectorMaking) {
        String projectInfoId = projectDirectorMaking.getProjectid();
        if(StringUtil.isNullOrSpace(projectInfoId)){
            throw new ScpException("projectid can not be null");
        }
        String stageTableName = "tbl_run_project_directormaking";
        String stageId = projectInfoService.getStageIdByProjectId(stageTableName, projectInfoId);
        if(StringUtil.isNullOrSpace(stageId)){
            projectDirectorMakingMapper.insert(
                    (ProjectDirectorMaking) CommonRunUtil.saveOrUpdateData(projectDirectorMaking, currentUser)
            );
            updateProjectStage(projectInfoId);
        }else{
            projectDirectorMaking.setId(stageId);
            projectDirectorMakingMapper.updateById(
                    (ProjectDirectorMaking) CommonRunUtil.saveOrUpdateData(projectDirectorMaking, currentUser)
            );
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult tenderOffer(ProjectTenderOffer projectTenderOffer) {
        String projectInfoId = projectTenderOffer.getProjectid();
        if(StringUtil.isNullOrSpace(projectInfoId)){
            throw new ScpException("projectid can not be null");
        }
        String stageTableName = "tbl_run_project_tenderoffer";
        String stageId = projectInfoService.getStageIdByProjectId(stageTableName, projectInfoId);
        if(StringUtil.isNullOrSpace(stageId)){
            projectTenderOfferMapper.insert(
                    (ProjectTenderOffer) CommonRunUtil.saveOrUpdateData(projectTenderOffer, currentUser)
            );
            updateProjectStage(projectInfoId);
        }else{
            projectTenderOffer.setId(stageId);
            projectTenderOfferMapper.updateById(
                    (ProjectTenderOffer) CommonRunUtil.saveOrUpdateData(projectTenderOffer, currentUser)
            );
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult ht(ProjectHt projectHt) {
        String projectInfoId = projectHt.getProjectid();
        if(StringUtil.isNullOrSpace(projectInfoId)){
            throw new ScpException("projectid can not be null");
        }
        String stageTableName = "tbl_run_project_ht";
        String stageId = projectInfoService.getStageIdByProjectId(stageTableName, projectInfoId);
        if(StringUtil.isNullOrSpace(stageId)){
            projectHtMapper.insert(
                    (ProjectHt) CommonRunUtil.saveOrUpdateData(projectHt, currentUser)
            );
            updateProjectStage(projectInfoId);
        }else{
            projectHt.setId(stageId);
            projectHtMapper.updateById(
                    (ProjectHt) CommonRunUtil.saveOrUpdateData(projectHt, currentUser)
            );
        }
        return AjaxResult.success();
    }
}
